package com.example.newsapp.ui.navigation

import android.util.Log
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.example.newsapp.data.compositionLocal.LocalDrawerEnabled
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.database.NewsItems
import com.example.newsapp.data.`object`.PageNavigation
import com.example.newsapp.data.`object`.StateSave
import com.example.newsapp.ui.screens.mainScreens.AccountScreen
import com.example.newsapp.ui.screens.mainScreens.NewsScreen
import com.example.newsapp.ui.screens.mainScreens.PictureScreen
import com.example.newsapp.ui.screens.mainScreens.WeatherScreen
import com.example.newsapp.ui.screens.otherScreens.NewsDetailsScreen
import com.example.newsapp.ui.screens.otherScreens.SearchResultScreen
import com.example.newsapp.ui.screens.otherScreens.SearchScreen
import com.example.newsapp.ui.screens.otherScreens.ViewImageScreen
import com.google.gson.Gson
import kotlinx.coroutines.launch

// 底部导航
@Composable
fun RuleNavigation(modifier: Modifier = Modifier) {
    val gson = Gson()
    var newsItem = NewsItems(0, "", "", "", "", 0, "")
    var drawable = LocalDrawerEnabled.current
    var scope = rememberCoroutineScope()
    // 导航图
    NavHost (
        navController = LocalNavController.current,
        startDestination = PageNavigation.newsScreen
    ) {
        // 新闻界面
        composable(
            route = PageNavigation.newsScreen,
            // 进入效果
            enterTransition = {
                scope.launch {
                    drawable.close()
                }
                slideInHorizontally(
                    initialOffsetX = { -it }
                )
            },
            // 出去效果
            exitTransition = {
                slideOutHorizontally(
                    targetOffsetX = { -it }
                )
            }
        ) {
            NewsScreen()
        }
        // 图片界面
        composable(
            route = PageNavigation.pictureScreen,
            // 进入效果
            enterTransition = {
                scope.launch {
                    drawable.close()
                }
                slideInHorizontally(
                    initialOffsetX = {
                        when(StateSave.previousSelect) {
                            0 -> it
                            2 -> -it
                            3 -> -it
                            else -> 0
                        }
                    }
                )
            },
            // 出去效果
            exitTransition = {
                slideOutHorizontally(
                    targetOffsetX = {
                        when(StateSave.currentSelect) {
                            0 -> it
                            2 -> -it
                            3 -> -it
                            else -> 0
                        }
                    }
                )
            }
        ) {
            PictureScreen()
        }
        // 天气界面
        composable(
            route = PageNavigation.weatherScreen,
            // 进入效果
            enterTransition = {
                scope.launch {
                    drawable.close()
                }
                slideInHorizontally(
                    initialOffsetX = {
                        when(StateSave.previousSelect) {
                            0 -> it
                            1 -> it
                            3 -> -it
                            else -> 0
                        }
                    }
                )
            },
            // 出去效果
            exitTransition = {
                slideOutHorizontally(
                    targetOffsetX = {
                        when(StateSave.currentSelect) {
                            0 -> it
                            1 -> it
                            3 -> -it
                            else -> 0
                        }
                    }
                )
            }
        ) {
            WeatherScreen()
        }
        // 我的界面
        composable(
            route = PageNavigation.accountScreen,
            // 进入效果
            enterTransition = {
                scope.launch {
                    drawable.close()
                }
                slideInHorizontally(
                    initialOffsetX = { it }
                )
            },
            // 出去效果
            exitTransition = {
                slideOutHorizontally(
                    targetOffsetX = { it }
                )
            }
        ) {
            AccountScreen()
        }
        // 搜索时的界面
        composable(
            route = PageNavigation.searchFrame,
            // 进入效果
            enterTransition = {
                StateSave.isSearchPage = true
                slideInHorizontally(
                    animationSpec = tween(
                        delayMillis = 300
                    ),
                    initialOffsetX = { it }
                )
            },
            // 出去效果
            exitTransition = {
                StateSave.gesturesEnable = true
                slideOutHorizontally(
                    targetOffsetX = { it }
                )
            }
        ) {
            SearchScreen()
        }
        // 新闻详情界面
        composable(
            route = "${PageNavigation.newsDetailsScreen}?newsItemJson={newsItemJson}",
            // 进入效果
            enterTransition = {
                slideInHorizontally(
                    animationSpec = tween(
                       delayMillis = 1000
                    ),
                    initialOffsetX = { it }
                )
            },
            // 出去效果
            exitTransition = {
                StateSave.gesturesEnable = true
                slideOutHorizontally(
                    targetOffsetX = { it }
                )
            }
        ) {
            val newsItemJson = it.arguments?.getString("newsItemJson")
            if (newsItemJson != null) {
                newsItem = gson.fromJson(newsItemJson, NewsItems::class.java)
            }
            NewsDetailsScreen(newsItem)
            StateSave.isNewsDetail = true
        }
        // 搜索结果界面
        composable(
            route = "${PageNavigation.searchResult}?title={title}",
            // 进入效果
            enterTransition = {
                slideInHorizontally(
                    animationSpec = tween(
                        delayMillis = 1000
                    ),
                    initialOffsetX = { -it }
                )
            },
            // 出去效果
            exitTransition = {
                StateSave.gesturesEnable = true
                slideOutHorizontally(
                    targetOffsetX = { -it }
                )
            }
        ) {
            SearchResultScreen(it.arguments?.getString("title"))
        }
        // 全屏查看图片界面
        composable(
            route = "${PageNavigation.viewImage}?title={title}&url={url}",
            // 进入效果
            enterTransition = {
                slideInHorizontally(initialOffsetX = { -it })
            },
            // 出去效果
            exitTransition = {
                slideOutHorizontally(targetOffsetX = { -it })
            }
        ) {
            ViewImageScreen(it.arguments?.getString("title"), it.arguments?.getString("url"))
        }
    }
}